/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.order.service.impl;

import me.zhengjie.modules.order.domain.OrderDetail;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.order.service.OrderDetailService;
import me.zhengjie.modules.order.domain.vo.OrderDetailQueryCriteria;
import me.zhengjie.modules.order.mapper.OrderDetailMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author yiwei.he
* @date 2024-04-22
**/
@Service
@RequiredArgsConstructor
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {

    private final OrderDetailMapper orderDetailMapper;

    @Override
    public PageResult<OrderDetail> queryAll(OrderDetailQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(orderDetailMapper.findAll(criteria, page));
    }

    @Override
    public List<OrderDetail> queryAll(OrderDetailQueryCriteria criteria){
        return orderDetailMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(OrderDetail resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrderDetail resources) {
        OrderDetail orderDetail = getById(resources.getId());
        orderDetail.copy(resources);
        saveOrUpdate(orderDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<OrderDetail> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderDetail orderDetail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单表id", orderDetail.getOId());
            map.put("商品id", orderDetail.getPId());
            map.put("商品种类id (规格)", orderDetail.getKId());
            map.put("商品名", orderDetail.getName());
            map.put("商品封面", orderDetail.getCover());
            map.put("商品数量", orderDetail.getNum());
            map.put("商品单价", orderDetail.getPrice());
            map.put("商品总价", orderDetail.getTotalPrice());
            map.put("1-未售后 2-售后中 3-已售后", orderDetail.getSoldStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<OrderDetail> list(OrderDetailQueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(orderDetailMapper.findAll(criteria, page));
    }
}